﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Animation
{
    public partial class Form1 : Form
    {

        int i; // steps counter

        double[,] coords = {{3.93487, 0.71891}, {3.81035, 1.21705}, {3.70915, 1.49741}, {3.67397,
   1.58176}, {3.71944, 1.47165}, {3.83073, 1.15131}, {3.95428, 
  0.603034}, {3.99653, -0.166605}, {3.84603, -1.09912}, {3.41773, 
-2.07826}, {2.68936, -2.96097}, {1.69506, -3.62309}, {0.48158, 
-3.9709}, {-0.919209, -3.89295}, {-2.35405, -3.23396}, {-3.35984, 
-2.1706}, {-3.85354, -1.07248}, {-3.99968, -0.0508219}, {-3.90489, 
  0.867101}, {-3.63653, 1.66603}, {-3.24688, 2.33619}, {-2.78056, 
  2.8755}, {-2.27451, 3.29038}, {-1.75613, 3.59388}, {-1.24268, 
  3.80207}, {-0.742185, 3.93054}, {-0.255478, 3.99183}, {0.221554, 
  3.99386}, {0.69564, 3.93905}, {1.17374, 3.82391}, {1.66036, 
  3.63912}, {2.15429, 3.37032}, {2.64526, 3.00044}, {3.11131, 
  2.51392}, {3.51872, 1.90226}, {3.82547, 1.16865}, {3.9865, 
  0.328409}, {3.95544, -0.595378}, {3.67612, -1.57674}, {3.05583, 
-2.58106}, {1.93207, -3.50244}, {0.37477, -3.9824}, {-1.03978, 
-3.8625}, {-2.16883, -3.36098}, {-3.03928, -2.60053}, {-3.64196, 
-1.65413}, {-3.95337, -0.608965}, {-3.97718, 0.426629}, {-3.76437, 
  1.35259}, {-3.3999, 2.10729}, {-2.97425, 2.67466}, {-2.56151, 
  3.07224}, {-2.21043, 3.33377}, {-1.94545, 3.49503}, {-1.77284, 
  3.58567}, {-1.68784, 3.62646}, {-1.68065, 3.62979}, {-1.74029, 
  3.60158}, {-1.85655, 3.54306}, {-2.02077, 3.45203}, {-2.22581, 
  3.32352}, {-2.46546, 3.14984}, {-2.73342, 2.92035}, {-3.02171, 
  2.62093}, {-3.31837, 2.23348}, {-3.60375, 1.7358}, {-3.84485, 
  1.10324}, {-3.98738, 
  0.31744}, {-3.9538, -0.606207}, {-3.67412, -1.58141}, {-3.13891, 
-2.47936}, {-2.39567, -3.20324}, {-1.51872, -3.70047}, {-0.603652, 
-3.95419}, {0.238789, -3.99287}, {0.906373, -3.89596}, {1.3303, 
-3.77231}, {1.48237, -3.71518}, {1.40601, -3.74475}, {1.30663, 
-3.78057}, {1.38297, -3.75332}, {1.65313, -3.64241}, {2.05829, 
-3.42979}, {2.5232, -3.10378}, {2.97611, -2.67259}, {3.36301, 
-2.16568}, {3.65525, -1.62455}, {3.84831, -1.09111}, {3.95479, 
-0.599709}, {3.9962, -0.174217}, {3.99633, 0.171227}, {3.97683, 
  0.429933}, {3.95474, 0.600057}, {3.94137, 0.682337}, {3.94206, 
  0.678372}, {3.95634, 0.589417}, {3.97835, 0.415612}, {3.99697, 
  0.155538}, {3.9953, -0.193829}, {3.94906, -0.636306}, {3.82398, 
-1.17354}, {3.57348, -1.79729}, {3.14265, -2.47462}, {2.48905, 
-3.13124}, {1.62248, -3.65617}, {0.630674, -3.94997}, {-0.356304, 
-3.9841}, {-1.2406, -3.80275}, {-1.99011, -3.4698}, {-2.6157, 
-3.02624}, {-3.1383, -2.48013}, {-3.5672, -1.80972}, {-3.87707, 
-0.984054}, {-3.999, -0.089481}, {-3.95062, 0.626586}, {-3.851, 
  1.08156}, {-3.7817, 1.30336}, {-3.78021, 1.30768}, {-3.84961, 
  1.08651}, {-3.95133, 
  0.622105}, {-3.99907, -0.0864488}, {-3.8755, -0.990215}, {-3.47924, 
-1.97355}, {-2.77813, -2.87784}, {-1.82408, -3.55988}, {-0.711036, 
-3.9363}, {0.48323, -3.9707}, {1.71455, -3.61391}, {2.88671, 
-2.76892}, {3.67151, -1.58746}, {3.97125, -0.478684}, {3.97403, 
  0.455054}, {3.80725, 1.22671}, {3.54459, 1.85362}, {3.2368, 
  2.35013}, {2.9231, 2.73047}, {2.6333, 3.01093}, {2.38613, 
  3.21036}, {2.18746, 3.34888}, {2.03203, 3.44541}, {1.9088, 
  3.51518}, {1.80686, 3.56865}, {1.71865, 3.61196}, {1.64036, 
  3.64818}, {1.57091, 3.67862}, {1.51072, 3.70375}, {1.46069, 
  3.72376}, {1.4216, 3.73886}, {1.39362, 3.74938}, {1.37614, 
  3.75583}, {1.36766, 3.75892}, {1.36588, 3.75957}, {1.36788, 
  3.75884}, {1.37058, 3.75786}, {1.37149, 3.75753}, {1.36995, 
  3.75809}, {1.36876, 3.75852}, {1.37584, 3.75594}, {1.40467, 
  3.74525}, {1.47218, 3.71923}, {1.59391, 3.66871}, {1.77901, 
  3.58261}, {2.02771, 3.44796}, {2.33181, 3.25002}, {2.67647, 
  2.97263}, {3.04128, 2.59819}, {3.39988, 2.10732}, {3.71709, 
  1.47759}, {3.94153, 
  0.681431}, {3.9878, -0.312213}, {3.71164, -1.4912}, {3.01447, 
-2.62925}, {2.03927, -3.44113}, {0.932993, -3.88967}, {-0.226057, 
-3.99361}, {-1.34829, -3.76592}, {-2.31848, -3.25955}, {-3.03929, 
-2.60053}, {-3.48561, -1.96228}, {-3.70815, -1.49989}, {-3.78002, 
-1.30823}, {-3.73492, -1.4319}, {-3.5298, -1.88162}, {-3.11587, 
-2.50826}, {-2.66642, -2.98165}, {-2.32222, -3.25688}, {-2.09264, 
-3.40894}, {-1.96559, -3.48374}, {-1.92979, -3.5037}, {-1.97133, 
-3.4805}, {-2.06346, -3.42668}, {-2.15644, -3.36894}, {-2.18344, 
-3.3515}, {-2.08614, -3.41292}, {-1.82641, -3.55868}, {-1.36795, 
-3.75882}, {-0.645587, -3.94756}, {0.451388, -3.97445}, {1.72786, 
-3.60756}, {2.55479, -3.07783}}; //array of first ball's coordinates

        double[,] coords2 = {{8.72244, -0.72301}, {8.04837, -1.4361}, {7.16133, -2.11956}, 
{6.14365, -2.76572}, {5.03659, -3.35174}, {3.87911, -3.84845}, 
{2.71962, -4.24213}, {1.6035, -4.55675}, {0.545992, -4.85542}, 
{-0.479148, -5.21104}, {-1.51293, -5.67036}, {-2.56537, -6.24009}, 
{-3.58171, -6.8846}, {-4.4049, -7.47764}, {-4.74286, -7.6264}, 
{-4.59706, -7.01511}, {-4.31384, -6.05125}, {-4.01353, -5.0508}, 
{-3.71236, -4.12919}, {-3.41025, -3.32885}, {-3.10662, -2.66184}, 
{-2.8021, -2.12446}, {-2.49759, -1.70464}, {-2.19315, -1.38698}, 
{-1.88763, -1.15616}, {-1.57887, -0.998957}, {-1.26422, -0.905354}, 
{-0.941178, -0.869067}, {-0.607925, -0.888036}, {-0.263884, 
-0.96495}, {0.0897336, -1.10779}, {0.449415, -1.33005}, {0.809031, 
-1.65018}, {1.16012, -2.08965}, {1.49334, -2.66915}, {1.80151, 
-3.40339}, {2.08341, -4.29525}, {2.34692, -5.32958}, {2.60944, 
-6.46163}, {2.89155, -7.57836}, {3.16335, -8.34847}, {3.12986, 
-8.15487}, {2.62541, -7.26344}, {1.90991, -6.25301}, {1.16072, 
-5.31347}, {0.464577, -4.50657}, {-0.150215, -3.85488}, {-0.703656, 
-3.35279}, {-1.24124, -2.9641}, {-1.80136, -2.63029}, {-2.39436, 
-2.2916}, {-3.0033, -1.9082}, {-3.59884, -1.46959}, {-4.15503, 
-0.990257}, {-4.65765, -0.498182}, {-5.10462, -0.0239601}, {-5.50238, 
  0.405757}, {-5.86161, 0.770555}, {-6.19415, 1.05605}, {-6.51108, 
  1.25269}, {-6.82178, 1.3545}, {-7.13336, 1.35798}, {-7.4502, 
  1.26146}, {-7.77336, 1.0647}, {-8.09911, 0.769075}, {-8.41589, 
  0.378129}, {-8.69763, -0.101136}, {-8.89147, -0.657198}, {-8.90855, 
-1.27733}, {-8.65976, -1.96004}, {-8.13318, -2.71866}, {-7.38267, 
-3.56352}, {-6.45552, -4.49294}, {-5.36121, -5.49224}, {-4.07324, 
-6.52395}, {-2.54948, -7.50942}, {-0.767414, -8.31099}, {1.23001, 
-8.70881}, {3.24468, -8.3944}, {4.89778, -7.2596}, {6.00514, 
-5.66003}, {6.6437, -3.94926}, {6.93068, -2.3074}, {6.96918, 
-0.816148}, {6.84384, 0.496116}, {6.61755, 1.63011}, {6.33196, 
  2.59863}, {6.01219, 3.41639}, {5.67273, 4.09589}, {5.32188, 
  4.64684}, {4.96455, 5.07659}, {4.60362, 5.39049}, {4.24083, 
  5.59186}, {3.87728, 5.68193}, {3.51393, 5.66001}, {3.15196, 
  5.52429}, {2.79288, 5.27305}, {2.43805, 4.9063}, {2.0876, 
  4.42793}, {1.73826, 3.84837}, {1.37992, 3.18841}, {0.991126, 
  2.48424}, {0.535985, 1.79214}, {-0.0314439, 1.18699}, {-0.750428, 
  0.74489}, {-1.63321, 0.50815}, {-2.6595, 0.453837}, {-3.78873, 
  0.499226}, {-4.9736, 0.542529}, {-6.16315, 0.497337}, {-7.2941, 
  0.300036}, {-8.26432, -0.0959443}, {-8.86948, -0.708655}, 
{-8.81259, -1.44202}, {-8.13272, -2.11386}, {-7.13451, -2.6892}, 
{-5.97965, -3.18763}, {-4.73487, -3.60034}, {-3.44974, -3.89748}, 
{-2.18455, -4.05534}, {-1.00232, -4.08888}, {0.0676, -4.06462}, 
{1.05932, -4.07152}, {2.05121, -4.17302}, {3.10668, -4.3891}, 
{4.22796, -4.71499}, {5.34518, -5.1375}, {6.29806, -5.61176}, 
{6.75331, -5.939}, {6.45607, -5.74031}, {5.78695, -5.13736}, 
{5.03052, -4.43205}, {4.25651, -3.75307}, {3.47964, -3.14596}, 
{2.70298, -2.62129}, {1.92925, -2.16976}, {1.16563, -1.76881}, 
{0.4261, -1.38946}, {-0.269284, -1.00593}, {-0.89933, -0.605161}, 
{-1.44828, -0.190221}, {-1.90964, 0.223862}, {-2.28546, 
  0.61745}, {-2.58311, 0.971937}, {-2.81199, 1.27233}, {-2.98121, 
  1.5077}, {-3.09835, 1.67068}, {-3.16876, 1.7568}, {-3.19529, 
  1.76394}, {-3.17808, 1.69208}, {-3.11463, 1.54327}, {-2.99972, 
  1.32205}, {-2.82574, 1.03607}, {-2.58308, 0.697046}, {-2.26121, 
  0.321907}, {-1.85063, -0.0665493}, {-1.34599, -0.440305}, 
{-0.749937, -0.769665}, {-0.0759476, -1.0305}, {0.652879, -1.21318}, 
{1.40993, -1.3279}, {2.17292, -1.40185}, {2.92848, -1.47024}, 
{3.67188, -1.56705}, {4.40386, -1.7195}, {5.12719, -1.94592}, 
{5.84347, -2.25489}, {6.54901, -2.64312}, {7.22561, -3.08882}, 
{7.81163, -3.53375}, {8.1173, -3.85557}, {7.83213, -3.96721}, 
{7.00653, -4.01235}, {5.9277, -4.11969}, {4.76328, -4.31994}, 
{3.57875, -4.61695}, {2.36662, -5.00591}, {1.06544, -5.45556}, 
{-0.390646, -5.88926}, {-2.0129, -6.20373}, {-3.74304, -6.3081}, 
{-5.46983, -6.12126}, {-6.97755, -5.50281}, {-7.78545, -4.29572}, 
{-7.66301, -2.79702}, {-6.96159, -1.39241}, {-5.93398, -0.208308}, 
{-4.69577, 0.70506}, {-3.31493, 1.30061}, {-1.85587, 
  1.51817}, {-0.408416, 1.29146}, {0.902679, 
  0.586029}, {1.95798, -0.549959}, {2.7086, -1.99502}, {3.17313, 
-3.62611}, {3.37165, -5.35136}, {3.26494, -7.06329}, {2.68844, 
-8.44609}, {1.53199, -8.60372}, {0.341204, -7.56113}}; //array of second ball's coordinates

        Point[] curve = new Point[200]; //array of first ball's points
        Point[] curve2 = new Point[200]; //array of second ball's point

        public Form1()
        {
            InitializeComponent(); //loading GUI
            timer1.Interval = 100; //setting timer interval

            for (int j = 0; j < 200; ++j) //filling the points' arrays
            {
                curve[j] = new Point(207 + (int)(20 * coords[j, 0]), 207 - (int)(20 * coords[j, 1]));
                curve2[j] = new Point(205 + (int)(20 * coords2[j, 0]), 205 - (int)(20 * coords2[j, 1]));
            }
        }

        private void DoDraw()
        {
            pictureBox1.Invalidate(); //getting frame ready to repaint
            pictureBox1.Update(); //repainting frame
        }



        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics; //getting graphics object from event arguments

            g.DrawLine(new Pen(Color.Black), new Point(200, 200), curve[i+1]); //drawing first needle
            g.DrawLine(new Pen(Color.Black), curve[i + 1], curve2[i + 1]); //drawing second needle
            g.DrawCurve(new Pen(Color.Blue), curve.Take(i + 2).ToArray()); //drawing first trajectory
            g.DrawCurve(new Pen(Color.Green), curve2.Take(i + 2).ToArray()); //drawing second trajectory
            g.FillEllipse(Brushes.Blue, new Rectangle(curve[i + 1].X - 7, curve[i + 1].Y - 7, 14, 14)); //drawing first ball
            g.FillEllipse(Brushes.Green, new Rectangle(curve2[i + 1].X - 5, curve2[i + 1].Y - 5, 10, 10)); //drawing second ball
            ++i; //increasing counter of steps

            if (i == coords.Length * 0.5 - 2) //checking array bounds
            {
                i = 0;
                timer1.Stop();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DoDraw(); //calling the repainting function
        }

        private void button1_Click(object sender, EventArgs e) //starting or stopping the process
        {
            if (timer1.Enabled)
                timer1.Enabled = false;
            else
                timer1.Enabled = true;
        }
    }
}
